package corp.sunny.sso.service;

import javax.jws.WebService;
import javax.security.auth.login.LoginException;

import org.apache.log4j.Logger;

import corp.sunny.commons.util.security.JAASPrincipal;
import corp.sunny.commons.util.security.JAASSerializer;
import corp.sunny.commons.util.security.JAASSerializerException;
import corp.sunny.commons.util.security.JAASUtil;
import corp.sunny.commons.util.spring.SpringUtil;

import corp.sunny.wsdl.authenticateservice.DoLoginRequest;
import corp.sunny.wsdl.authenticateservice.DoLoginResponse;
import corp.sunny.wsdl.authenticateservice.ServiceFault;

/**
 * @File:AuthenticateServiceImpl.java
 * @Date:Jun 14, 2011
 * @Author:Subhash Bavi
 * 
 * @Description:
 * @Copyright Sunny Corporation - 2011
 */

@WebService(endpointInterface = "corp.sunny.sso.service.AuthenticateService", serviceName = "AuthenticateService", wsdlLocation = "/wsdl/AuthenticateService-Soap11.wsdl")
public class AuthenticateServiceImpl implements AuthenticateService {

	final Logger LOG = Logger.getLogger(AuthenticateServiceImpl.class);

	/**
	 * Initialize the Spring context. This is required for all the indivisual
	 * Services
	 */
	DoLoginResponse loginResponse = (DoLoginResponse) SpringUtil
			.getBean("doLoginResponse");
	ServiceFault serviceFault = (ServiceFault) SpringUtil
			.getBean("serviceFault");

	@Override
	public DoLoginResponse doLogin(DoLoginRequest loginRequest) {

		String user = null;
		String passwd = null;
		String secureSession = null;
		final String _BLANK = "";

		JAASPrincipal jaasPrincipal = null;

		// Check if loginRequest is null
		if (null == loginRequest) {
			serviceFault.setErrorCode("LOGIN_REQUEST_NULL");
			serviceFault.setErrorDesc("loginRequest input is null");
			LOG.warn("loginRequest input is null");
			loginResponse.setServiceFault(serviceFault);
			return loginResponse;
		}

		user = loginRequest.getUser();
		passwd = loginRequest.getPasswd();

		// Check if the input parameters are passed in the request object
		if (null == user || _BLANK.equals(user) || null == passwd
				|| _BLANK.equals(passwd)) {
			serviceFault.setErrorCode("LOGIN_REQUEST_PARAM_NULL");
			serviceFault.setErrorDesc("user/passwd cannot be null or empty");
			LOG.warn("user/passwd cannot be null or empty");
			loginResponse.setServiceFault(serviceFault);
			return loginResponse;
		}

		try {
			jaasPrincipal = (JAASPrincipal) JAASUtil.getJAASPrincipal(user,
					passwd);
			LOG.info("jaasPricipal: " + jaasPrincipal);

			secureSession = JAASSerializer.getSerializedObject(jaasPrincipal);
			LOG.info("secureSession created: " + secureSession);
			loginResponse.setSecureSession(secureSession);
		} catch (LoginException e) {
			serviceFault.setErrorCode("LOGIN_FAILURE");
			serviceFault.setErrorDesc("Login failed : " + e.getMessage());
			LOG.error("Login failed", e);
			loginResponse.setServiceFault(serviceFault);
			return loginResponse;
		} catch (JAASSerializerException e) {
			serviceFault.setErrorCode("SECURE_SESSION_FAILURE");
			serviceFault.setErrorDesc("Failed to serialize principal object : "
					+ e.getMessage());
			LOG.error("Failed to serialize principal object", e);
			loginResponse.setServiceFault(serviceFault);
			return loginResponse;
		} catch (Exception e) {
			serviceFault.setErrorCode("UNKNOWN_FAILURE");
			serviceFault.setErrorDesc("Failed to execute the service : "
					+ e.getMessage());
			LOG.error("Failed to execute the service", e);
			loginResponse.setServiceFault(serviceFault);
			return loginResponse;
		}
		return loginResponse;
	}
}
